package com.example.cloud.resolver;

import com.example.cloud.entity.User;
import com.example.cloud.mapper.UserMapper;
import com.example.cloud.util.PageRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class UserResolver {
    
    private final UserMapper userMapper;
    
    /**
     * 查询单个用户
     */
    @QueryMapping
    public User user(@Argument Long id) {
        return userMapper.findById(id);
    }
    
    /**
     * 查询用户列表
     */
    @QueryMapping
    public List<User> users(@Argument User input) {
        // 由于findByPage需要PageRequest参数，这里创建一个默认的PageRequest
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(1);
        pageRequest.setPageSize(100); // 设置一个合理的默认值
        return userMapper.findByPage(input, pageRequest);
    }
    
    /**
     * 查询用户总数
     */
    @QueryMapping
    public Integer userCount() {
        return userMapper.count(null);
    }
    
    /**
     * 创建用户
     */
    @MutationMapping
    public User createUser(@Argument("input") User input) {
        userMapper.insert(input);
        return userMapper.findById(input.getId());
    }
    
    /**
     * 更新用户
     */
    @MutationMapping
    public User updateUser(@Argument("input") User input) {
        userMapper.update(input);
        return userMapper.findById(input.getId());
    }
    
    /**
     * 删除用户
     */
    @MutationMapping
    public Boolean deleteUser(@Argument Long id) {
        return userMapper.deleteById(id) > 0;
    }
} 